<?php
// 应用公共文件
use think\facade\Cache;
use think\facade\Db;
use think\facade\Event;

if (!function_exists('sysconf')) {
	/**
     * 配置系统参数
     * @param string $name 参数名称
     * @param boolean $value 无值为获取
     * @return string|boolean
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    function sysconf($name, $value = null)
    {
        static $data = [];
        list($field, $raw) = explode('|', "{$name}|");
        if ($value !== null) {
            Cache::set("system_config", $data);
            list($row, $data) = [['name_field' => $field, 'value' => $value], []];
            $wh = Db::name('system_config')->where('name_field','=',$field)->find();
            return Db::name('system_config')->where($wh)->save($row);
        }
        if (empty($data)) {
            $data = Cache::get('system_config');
            if (empty($data)) {
                $data = Db::name('system_config')->column('value','name_field');
                Cache::set("system_config", $data);
            }
        }
        if (isset($data[$field])) {
            if (strtolower($raw) === 'raw') {
                return $data[$field];
            } else {
                return htmlspecialchars($data[$field]);
            }
        } else {
            return '';
        }
    }   
}   
/**
 * 事件
 */
function event($event, $args = [], $once = true)
{
    $res = Event::trigger($event, $args);
    if (is_array($res)) {
        $res = array_filter($res);
        sort($res);
    }
    //只返回一个结果集
    if ($once) {
        return isset($res[ 0 ]) ? $res[ 0 ] : [];
    }
    return $res;
}